<template>
  <div class="newshop-usermaterial">
    <!-- 头部导航 -->
    <div class="el-header">
      <el-menu router="" background-color="#eee" text-color="#303133" class="el-menu-demo" mode="horizontal">
        <el-menu-item index="/main">首页</el-menu-item>
        <el-menu-item index="/2">用户素材管理</el-menu-item>
      </el-menu>
    </div>
    <!-- 卡片试图 -->
    <el-card>
      <el-form :inline="true" :model="formInline" class="demo-form-inline" ref="usermaterialRef">
        <el-form-item label="素材名称" prop="name">
          <el-input v-model="formInline.name" placeholder="请输入素材名称"></el-input>
        </el-form-item>
        <el-form-item label="上传者ID" prop="userid">
          <el-input v-model="formInline.userid" placeholder="上传者ID"></el-input>
        </el-form-item>
        <el-form-item label="素材类别" prop="type">
          <el-select v-model="formInline.type" placeholder="素材类别">
            <el-option v-for="item in typelist" :key="item.id" :label="item.name" :value="item.name"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getselectusermaterial">查询</el-button>
          <el-button type="primary" @click="clearmaterial">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 表单内容区域 -->
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="material_name" label="素材名称" width="80px"> </el-table-column>
        <el-table-column label="素材图">
          <template slot-scope="scope">
            <img :src="scope.row.material_img" alt="" width="90" height="90" />
          </template>
        </el-table-column>
        <el-table-column prop="material_type" label="素材类别"> </el-table-column>
        <el-table-column prop="price" label="价格" width="80px"> </el-table-column>
        <el-table-column prop="userid" label="上传者ID" width="80px"></el-table-column>
        <el-table-column prop="mobile" label="联系电话"> </el-table-column>
        <el-table-column prop="audit_date" label="素材上架日期"> </el-table-column>
        <el-table-column prop="sales" label="售出次数"> </el-table-column>
        <el-table-column prop="profit" label="素材收益"> </el-table-column>
        <el-table-column label="操作">
          <!-- slot-scope="scope" -->
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" size="mini" @click="deletematerial(scope.row)">违规下架</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'NewshopUsermaterial',
  props: {},
  data() {
    return {
      value: '',
      tableData: [],
      formInline: {
        name: '',
        userid: '',
        type: ''
      },
      // 类别数据列表
      typelist: []
    }
  },

  components: {},

  computed: {},

  beforeMount() {},

  created() {
    this.getbusinesses()
    this.getusermaterial()
  },

  mounted() {},

  methods: {
    // 获取所有类别
    async getbusinesses() {
      const { data: res } = await this.$http.get('/businesses')
      this.typelist = res
    },
    // 查询所有审核通过的素材
    async getusermaterial() {
      const { data: res } = await this.$http.get(`/reciews?state_ne=${0}`)
      console.log(res)
      this.tableData = res
    },
    // 查询数据
    async getselectusermaterial() {
      this.tableData = []
      const str = this.formInline.name + this.formInline.userid + this.formInline.type
      // 判断 三个框里的内容是否为空
      if (str === '') {
        this.getusermaterial()
        return this.getbusinesses()
      }
      // 查询素材名称
      if (this.formInline.name !== '') {
        const { data: res } = await this.$http.get(`/reciews?material_name_like=${this.formInline.name}`)
        console.log(res, 1)
        this.tableData = res
      }
      // 查询上传者id
      if (this.formInline.userid !== '') {
        const { data: res2 } = await this.$http.get(`/reciews?userid_like=${this.formInline.userid}`)
        console.log(res2, 2)
        res2.forEach((element) => {
          if (!this.tableData.find((val) => element.id === val.id)) {
            console.log(element)
            this.tableData.push(element)
          }
        })
      }
      // 查询分类
      if (this.formInline.type !== '') {
        const { data: res3 } = await this.$http.get(`/reciews?material_type_like=${this.formInline.type}`)
        res3.forEach((element) => {
          // console.log(element)
          if (!this.tableData.find((val) => element.id === val.id)) {
            // console.log(element)
            this.tableData.push(element)
            // console.log(this.tableData)
          }
        })
      }
    },
    // 重置搜索表单
    clearmaterial() {
      this.$refs.usermaterialRef.resetFields()
      this.getbusinesses()
      this.getusermaterial()
    },
    // 违规下架
    deletematerial(val) {
      // console.log(val)
      this.$confirm('确定将此素材下架吗', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await this.$http.delete(`/reciews/${val.id}`)
          this.$message({
            type: 'success',
            message: '下架成功!'
          })
          this.getusermaterial()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消下架'
          })
          this.getusermaterial()
        })
    }
  },

  watch: {}
}
</script>
<style lang='less' scoped>
.newshop-usermaterial {
  background-color: #eee;
  width: 100%;
  .el-header {
    color: #333;
    padding: 0;
    .el-row {
      margin-top: 32px;
      padding-left: 20px;
    }
    .el-button {
      margin-left: 0;
    }
  }
  .materialname,
  .materialcategory,
  .userid {
    display: flex;
    .name,
    .category {
      width: 100px;
      margin-top: 10px;
    }
  }
  .btn {
    // margin: 20px 0 30px 0;
    display: flex;
    right: 0;
    margin: 30px 0 20px 0;
  }
}
</style>
